package com.har.modules.business.controller;

import com.har.common.utils.PageUtils;
import com.har.common.utils.R;
import com.har.common.validator.ValidatorUtils;
import com.har.modules.business.common.CodeConstant;
import com.har.modules.business.entity.TBannerEntity;
import com.har.modules.business.service.TBannerService;
import com.har.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * banner管理
 *
 * @author macwy
 * @email 
 * @date 2018-06-26 16:38:42
 */
@RestController
@RequestMapping("business/tbanner")
public class TBannerController {
    @Autowired
    private TBannerService tBannerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:tbanner:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = tBannerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:tbanner:info")
    public R info(@PathVariable("id") Long id){
        TBannerEntity tBanner = tBannerService.selectById(id);

        return R.ok().put("tBanner", tBanner);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:tbanner:save")
    public R save(@RequestBody TBannerEntity tBanner){

        if(null == tBanner.getBannerName()){
            return R.error("名称不能为空");
        }
        tBanner.setOperator(ShiroUtils.getUserEntity().getUsername());
        tBanner.setCreateTime(new Date());
        tBanner.setUpdateTime(new Date());
        tBanner.setStatus(CodeConstant.BANNER_STATUS.CLOSE.getCode());

        tBannerService.insert(tBanner);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:tbanner:update")
    public R update(@RequestBody TBannerEntity tBanner){
        ValidatorUtils.validateEntity(tBanner);
        tBanner.setOperator(ShiroUtils.getUserEntity().getUsername());
        tBanner.setUpdateTime(new Date());
        tBannerService.updateAllColumnById(tBanner);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresPermissions("business:tbanner:delete")
    public R delete(@PathVariable(value = "id") Long id){
        tBannerService.deleteBatchIds(Arrays.asList(id));

        return R.ok();
    }
}
